package com.zx.练习题.demo;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo5 {

    public static void main(String[] args) {
        AtomicBoolean isA = new AtomicBoolean(Boolean.TRUE);
        AtomicInteger num = new AtomicInteger(1);

        // A线程
        new Thread(() -> {
            while (true) {
                if (isA.get()) {
                    int i = num.get();
                    if (i == 10) {
                        break;
                    }
                    System.out.println(i);

                    num.incrementAndGet();// 先加1
                    isA.compareAndSet(Boolean.TRUE, Boolean.FALSE);//在变标志位
                }
            }
            System.out.println("A end");
        }).start();

        // B线程
        new Thread(() -> {
            while (true) {
                if (!isA.get()) {
                    int i = num.get();
                    System.out.println(num);
                    if (i == 10) {
                        isA.compareAndSet(Boolean.FALSE, Boolean.TRUE);//在变标志位
                        break;
                    } else {
                        num.incrementAndGet();
                        isA.compareAndSet(Boolean.FALSE, Boolean.TRUE);//在变标志位
                    }
                }
            }
            System.out.println("B end");
        }).start();

    }

}
